package com.kaoshi.controller;

import com.github.pagehelper.PageInfo;
import com.kaoshi.pojo.News;
import com.kaoshi.service.NewsService;
import com.kaoshi.utils.PageInfoRes;
import com.kaoshi.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//注解用于将Controller的方法返回的对象，通过适当的HttpMessageConverter转换为指定格式后，写入到Response对象的body数据区
@RestController
//跨域注解
@CrossOrigin
public class NewsController {
    @Autowired
    private NewsService newsService;

    @Autowired
    private PageInfoRes pageInfoRes;

    @PostMapping("/getNews")
    //其中pageNum为页码，pageSize为每页显示条数
    public PageInfoRes getNews(@RequestBody PageUtils pageUtils) {
        PageInfo<News> news = newsService.getNews(pageUtils.getPageNum(),pageUtils.getPageSize());
        return pageInfoRes.getPageInfoRes(news,new PageInfoRes<News>(),pageUtils.getPageNum(),pageUtils.getPageSize());
    }

    @GetMapping("/news/{id}")
    public News selectIdNews(@RequestBody @PathVariable("id") int id){
        News news = newsService.getIdNews(id);
        return news;
    }

    @PostMapping("/getLinkNews")
    //其中pageNum为页码，pageSize为每页显示条数
    public PageInfoRes getLinkNews(@RequestBody PageUtils pageUtils) {
        PageInfo<News> news = newsService.selectlinkTitleNews(pageUtils.getName(),pageUtils.getPageNum(),pageUtils.getPageSize());
        return pageInfoRes.getPageInfoRes(news,new PageInfoRes<News>(),pageUtils.getPageNum(),pageUtils.getPageSize());
    }

    @GetMapping("/getIdNews/{id}")
    public News getidNews(@PathVariable("id") int id) {
        News idNews = newsService.getIdNews(id);
        return idNews;
    }

    @DeleteMapping("/deletedIdNews/{id}")
    public int deletedidNews(@PathVariable("id") int id) {
        int i = newsService.deleteNew(id);
        return i;
    }

    @PutMapping("/putNews")
    public int putNews(@RequestBody News news) {
        int i = newsService.updateNew(news);
        return i;
    }

    @PostMapping("/addNews")
    public int addNews(@RequestBody News news) {
        int i = newsService.addNew(news);
        return i;
    }


}